// pages/user/recharge.js
var md5 =require("../../../utils/md5.js");
var server = require("../../../utils/server.js");
var search,up;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectArray: [{
      "id": "1",
      "text": "￥10000.00元",
      'money':10000
    }, {
      "id": "2",
      "text": "￥30000.00元",
      'money': 30000
    }, {
      "id": "3",
      "text": "￥50000.00元",
      'money': 50000
    }],
    selectArray2: [{
      "id": "1",
      "text": "支付宝"
    }]
  },

  getDate:function(e){
    var that = this;
    console.log(e)
    var id = e.detail.id;
    var money = that.data.selectArray;
    money  = money[id].money;
    var param = money + that.data.info.openid;
    var md5s = md5.hex_md5(param);
    server.postJSON("/cart/recharge",{money:money,openid:that.data.info.openid,md5s:md5s,name:getApp().globalData.userInfo.nickname,user_id:getApp().globalData.userInfo.user_id},function(e){
      console.log(e)
      getApp().globalData.wxdata = e.data.wdata;
      that.setData({
        order_id:e.data.order_id,
      })
    })
  },

  order_search:function(){
    var that = this;
    var times = 0;
    console.log("查询订单")
    search = setInterval(function(){
      times++;
      if(times >= 100){
        clearInterval(search);
      }
        server.postJSON("/Cart/order_search",{order_id:that.data.order_id,},function(e){
          console.log(e)
          if(e.data.code == 1){
            
            clearInterval(search);
          }
        })

    },3000)
  },

  pay: function () {
    clearInterval(search);
    clearInterval(up);
    var that = this;
    var app = getApp();
    that.order_search();
    var i = 0;
    up = setInterval(function () {
      if (i >= 50) {
        clearInterval(up);
      }
      server.postJSON("/Cart/rechargeStatus", { order_id: that.data.order_id, user_id: getApp().globalData.userInfo.user_id }, function (e) {
        console.log(e)
        if (e.data.status == 1) {
          getApp().globalData.userInfo = e.data.info;
          clearInterval(up);
        }
      })
    }, 3000)
    var wxdata = app.globalData.wxdata;
    var timeStamp = wxdata.timeStamp + "";
    var nonceStr = wxdata.nonceStr + "";
    var package1 = wxdata.package
    var sign = wxdata.sign;
    wx.requestPayment({

      'nonceStr': nonceStr,
      'package': package1,
      'signType': 'MD5',
      'timeStamp': timeStamp,
      'paySign': sign,
      'success': function (res) {
        console.log(res);
        wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 })
        setTimeout(function doHandler() {
          wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }, 2500);
      },
      'fail': function (res) {
        console.log(res);
        wx.showToast({ title: '支付失败', icon: 'success', duration: 2000 })
        setTimeout(function doHandler() {
          wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }, 2000);
      }
    })

    // update order
    /*var query = new AV.Query('Order');
    query.get(this.data.orderId).then(function (order) {
      order.set('status', 1);
      order.save();
      console.log('status: ' + 1);
    }, function (err) {
    	
    });*/
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  = this;
    var info = wx.getStorageSync("sysinfo");
    that.setData({
      info:info,
      money:getApp().globalData.userInfo.user_money,
    })
    console.log(info)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})